package io.renren.modules.meta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.meta.dao.SubjectDao;
import io.renren.modules.meta.entity.SubjectEntity;
import io.renren.modules.meta.service.SubjectService;
import io.renren.modules.subject.entity.BasePersons;
import io.renren.modules.subject.service.CriminalStrikePersonsService;
import io.renren.modules.subject.service.FraudulentPersonsService;
import io.renren.modules.subject.service.PenaltyPersonsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("subjectService")
public class SubjectServiceImpl extends ServiceImpl<SubjectDao, SubjectEntity> implements SubjectService {
@Autowired
private CriminalStrikePersonsService criminalStrikePersonsService;
@Autowired
private FraudulentPersonsService fraudulentPersonsService;
@Autowired
private PenaltyPersonsService penaltyPersonsService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SubjectEntity> page = this.page(
                new Query<SubjectEntity>().getPage(params),
                new QueryWrapper<SubjectEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Map<String, Object>> getTableInfoBySubjectName(String subjectName) {
        QueryWrapper<SubjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("subject_name", subjectName);
        queryWrapper.select("subject_id","subject_table");
        List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
        return  maps;
    }

    @Override
    public Map<String, Object> getSubjectByTableId(Long tableId, Long subjectId) {
        SubjectEntity subjectEntity = baseMapper.selectById(tableId);
        String subjectTable = subjectEntity.getSubjectTable();
        String sql="select * from "+subjectTable+ " where id="+subjectId;
        Map<String, Object> stringObjectMap = SqlRunner.db().selectOne(sql);
        return stringObjectMap;
    }

    @Override
    public BasePersons getPersonBySubjectName(String subjectName, Long subjectId) {
        QueryWrapper<SubjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("subject_name", subjectName);
        SubjectEntity subjectEntity = baseMapper.selectOne(queryWrapper);
        String subjectTable = subjectEntity.getSubjectTable();
        BasePersons person;
        if (subjectTable.equals("tb_penalty_persons")) {
            person = penaltyPersonsService.getById(subjectId);
        } else if (subjectTable.equals("tb_fraudulent_persons")) {
            person = fraudulentPersonsService.getById(subjectId);
        } else if (subjectTable.equals("tb_criminal_strike_persons")) {
            person = criminalStrikePersonsService.getById(subjectId);
        } else {
            throw new IllegalArgumentException();
        }
        return person;
    }

    @Override
    public Long getTableIdByTableName(String subjectTableName) {
        QueryWrapper<SubjectEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("subject_table", subjectTableName);
        SubjectEntity subjectEntity = baseMapper.selectOne(queryWrapper);
        return subjectEntity.getSubjectId();
    }

}